<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="银行流水号" prop="secret">
        <el-input
          v-model="queryParams.secret"
          placeholder="请输入银行流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动编码" prop="outlets">
        <el-input
          v-model="queryParams.outlets"
          placeholder="请输入活动编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户id" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行用户id" prop="bankUserId">
        <el-input
          v-model="queryParams.bankUserId"
          placeholder="请输入银行用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择领取状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领取时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 360px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="receiveList" @selection-change="handleSelectionChange">
      <el-table-column label="银行流水号" align="center" prop="secret" />
      <el-table-column label="活动编码" align="center" prop="outlets" />
      <el-table-column label="奖品编码" align="center" prop="phone" />
      <el-table-column label="openId" align="center" prop="openId" />
      <el-table-column label="银行用户id" align="center" prop="bankUserId" />
      <el-table-column label="领取状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="立减金" align="center" prop="type" />
      <el-table-column label="代金券id" align="center" prop="remark" />
      <el-table-column label="领取时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            v-if="scope.row.status != 1"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:receive:edit']"-->
<!--          >重发</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:receive:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改领取对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="bankUserId">
          <el-input v-model="form.bankUserId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户id" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="业务类型">
          <el-select v-model="form.type" placeholder="请选择业务类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="img">
          <el-input v-model="form.img" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="领取状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="领取状态" prop="secret">
          <el-input v-model="form.secret" placeholder="请输入领取状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceive, getReceive, delReceive, addReceive, updateReceive, exportReceive } from "@/api/system/receive";

export default {
  name: "Receive",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 领取表格数据
      receiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 领取状态数据字典
      statusOptions: [],
      // 业务类型字典
      typeOptions: [],
      usedOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orders: undefined,
        bankUserId: undefined,
        openId: undefined,
        phone: undefined,
        nickName: undefined,
        outlets:undefined,
        type: undefined,
        img: undefined,
        status: undefined,
        used: undefined,
        secret: undefined,
        typeName:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询领取列表 */
    getList() {
      this.getDicts("sys_reward").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("sys_bussiness_type").then(response => {
        this.typeOptions = response.data;
      });
      this.getDicts("sys_used_type").then(response => {
        this.usedOptions = response.data;
      });
      this.loading = true;
      listReceive(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.receiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    usedFormat(row, column) {
      return this.selectDictLabel(this.usedOptions, row.used);
    },
    typeFormat(row, column) {
      var actions = [];
      Object.keys(this.typeOptions).map((key) => {
        if (this.typeOptions[key].dictValue == ('' + row.type)) {
          actions.push(this.typeOptions[key].dictLabel+" "+this.typeOptions[key].remark);
          return false;
        }
      })
      return actions.join('');
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        receiveId: undefined,
        bankUserId: undefined,
        openId: undefined,
        phone: undefined,
        nickName: undefined,
        type: undefined,
        img: undefined,
        status: "0",
        secret: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange=[];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receiveId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加领取";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const receiveId = row.receiveId || this.ids
      this.$confirm('是否确认重新领取?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return getReceive(receiveId).then(response => {
          if (response.code === 200) {
            this.msgSuccess("发送成功");
            this.getList();
          } else {
            this.msgError(response.msg);
          }
        });
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.receiveId != undefined) {
            updateReceive(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addReceive(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const receiveIds = row.receiveId || this.ids;
      this.$confirm('是否确认删除该兑换记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReceive(receiveIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有领取数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportReceive(queryParams);
      }).then(response => {
        window.location.href = process.env.VUE_APP_BASE_API + "/common/download?fileName=" + encodeURI(response.msg) + "&delete=" + false;
      })
    }
  }
};
</script>
